﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using JQueryDataTables.Models;
using TireBias.Models;
using System.Data;
using WinNemoLib;

namespace TireBias.Controllers
{
    public class BiasLossController : Controller
    {
        //
        // GET: /BiasLoss/

        public ActionResult Index(string biasno = "", string returnUrl = "", string tirecode = "", string toplot = "", string toproll = "")
        {
            ViewBag.Full = "F";
            ViewBag.Return = returnUrl.Replace("|", "&");

            TailModel getShift = new TailModel();
            DataTable dt = getShift.GET_SHIFT();
            TireBias.Models.SessionBag.Current.shift = dt.Rows[0]["shift"].ToString();
            TireBias.Models.SessionBag.Current.curdate = dt.Rows[0]["CurDate"];
            TireBias.Models.SessionBag.Current.biasno = biasno;
            TireBias.Models.SessionBag.Current.tirecode = tirecode;
            TireBias.Models.SessionBag.Current.toplot = toplot;
            TireBias.Models.SessionBag.Current.toproll = toproll;
            return View();
        }
        public JsonResult List_Loss(JQueryDataTableParamModel param)
        {
            LossModel m = new LossModel();
            IEnumerable<LossModel.RES_LOSS_DS> IElist;

            IElist = m.SEL_LOSS(WinNemoLib.DateUtil.ToDisplayDate(TireBias.Models.SessionBag.Current.curdate,
                                WinNemoLib.DateUtil.eLanguage.EN), param, SessionBag.Current.shift
                                , SessionBag.Current.biasno, TireBias.Models.SessionBag.Current.tirecode
                                , TireBias.Models.SessionBag.Current.toplot, TireBias.Models.SessionBag.Current.toproll
                );
            var display = IElist.Skip(param.iDisplayStart).Take(param.iDisplayLength);
            var result = from c in display select new[] {  c._Reason,  c._Start, c._End, c._LsTime };
            return Json(new
            {
                sEcho = param.sEcho,
                iTotalRecords = IElist.Count(),
                iTotalDisplayRecords = IElist.Count(),
                aaData = result
            },
                         JsonRequestBehavior.AllowGet);
        }

        public JsonResult Get_Detail()
        {
            LossModel rs = new LossModel();
            List<LossModel.LOSS_DS> list = rs.SEL_LOSS_MASTER("");
            return Json(list);
        }


        public JsonResult Get_Validate(string timefr, string timesec, string reasoncode,string text)
        {
            TopCordBomModel tire = new TopCordBomModel();

          
            Conn cnnTime = new Conn();

            Boolean timesF = cnnTime.GetDateTimeDiffCurrent(DateUtil.ToInternalDateTime(timefr));
            Boolean timesT = cnnTime.GetDateTimeDiffCurrent(DateUtil.ToInternalDateTime(timesec));
            if ((timesF) || (timesT))
            {
                return Json(new { success = 0, message = "ช่วงเวลาต้องน้อยกว่าปัจจุบัน" });
            }


            LossModel rs = new LossModel();
            List<LossModel.LOSS_DS> list = rs.SEL_LOSS_MASTER(reasoncode);
            if (list.Count <= 0)
            {
                return Json(new { success = 0, message = "ไม่พบข้อมูล Reason ที่ระบุ" });
            }

            return Json(new { success = 1, message = "" });
        }


        public JsonResult INS_LOSS_DATA(LossModel.RES_LOSS_DS theData)
        {
            try
            {

                LossModel mdUpd = new LossModel();
                
                if (mdUpd.Ins_Data(theData))
                {
                    return Json(new { success = 1, message = "บันทึกเรียบร้อย" });
                }
                else
                {
                    return Json(new { success = 0, message = "ไม่สามารถบันทึกได้" });
                }
            }
            catch (Exception ex)
            {
                WinNemoLib.Nemo.Error(ex);
                throw;
            }



        }

    }
}
